package com.bookocean.infrastructure.persistence.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.bookocean.controller.dto.BookDto;
import com.bookocean.controller.dto.BookQuoteDto;
import com.bookocean.infrastructure.persistence.po.BookPO;
import com.bookocean.infrastructure.persistence.po.BookQuotePO;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 书籍对象转换工具类
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@UtilityClass
public class BookConverter {

    /**
     * 复制选项配置
     */
    private static final CopyOptions COPY_OPTIONS = CopyOptions.create()
            .setIgnoreNullValue(false)
            .setIgnoreError(true);

    /**
     * BookPO 转换为 BookDto
     * 
     * @param bookPo BookPO 对象
     * @return BookDto 对象，如果输入为 null 则返回 null
     */
    public static BookDto toDto(BookPO bookPo) {
        if (bookPo == null) {
            return null;
        }
        
        BookDto bookDto = new BookDto();
        BeanUtil.copyProperties(bookPo, bookDto, COPY_OPTIONS);
        return bookDto;
    }

    /**
     * BookDto 转换为 BookPO
     * 
     * @param bookDto BookDto 对象
     * @return BookPO 对象，如果输入为 null 则返回 null
     */
    public static BookPO toPo(BookDto bookDto) {
        if (bookDto == null) {
            return null;
        }
        
        BookPO bookPo = new BookPO();
        BeanUtil.copyProperties(bookDto, bookPo, COPY_OPTIONS);
        return bookPo;
    }

    /**
     * BookPO 列表转换为 BookDto 列表
     * 
     * @param bookPoList BookPO 列表
     * @return BookDto 列表
     */
    public static List<BookDto> toDtoList(List<BookPO> bookPoList) {
        if (bookPoList == null || bookPoList.isEmpty()) {
            return new ArrayList<>();
        }
        
        return bookPoList.stream()
                .map(BookConverter::toDto)
                .collect(Collectors.toList());
    }

    /**
     * BookDto 列表转换为 BookPO 列表
     * 
     * @param bookDtoList BookDto 列表
     * @return BookPO 列表
     */
    public static List<BookPO> toPoList(List<BookDto> bookDtoList) {
        if (bookDtoList == null || bookDtoList.isEmpty()) {
            return new ArrayList<>();
        }
        
        return bookDtoList.stream()
                .map(BookConverter::toPo)
                .collect(Collectors.toList());
    }

    public static BookQuotePO toBookQuotePo(BookQuoteDto bookQuoteDto) {
        BookQuotePO bookQuotePo = new BookQuotePO();
        BeanUtil.copyProperties(bookQuoteDto, bookQuotePo, COPY_OPTIONS);
        return bookQuotePo;
    }

    public static BookQuoteDto toBookQuoteDto(BookQuotePO bookQuotePo) {
        BookQuoteDto bookQuoteDto = new BookQuoteDto();
        BeanUtil.copyProperties(bookQuotePo, bookQuoteDto, COPY_OPTIONS);
        return bookQuoteDto;
    }
}
